package com.fcscs.cservice.client.main.folio;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.FormPanel.LabelAlign;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.Validator;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.fcscs.core.common.MainPaginationSearchModel;
import com.fcscs.core.common.SearchResultsContainer;
import com.fcscs.core.common.authority.CServiceAuthority;
import com.fcscs.core.common.authority.UserAuthority;
import com.fcscs.core.gwt.common.AsyncCallbackListener;
import com.fcscs.core.gwt.common.CoreConstants;
import com.fcscs.core.gwt.common.SearchExecutor;
import com.fcscs.core.gwt.common.SessionContext;
import com.fcscs.core.gwt.common.ui.FcsFields.EasyClearCombobox;
import com.fcscs.core.gwt.common.ui.FcsFields.FormButton;
import com.fcscs.core.gwt.common.ui.FcsPanel;
import com.fcscs.core.gwt.common.ui.IEFriendlyDateField;
import com.fcscs.cservice.client.common.CServiceConstants;
import com.fcscs.cservice.client.common.controller.CServiceCommonServiceController;
import com.fcscs.cservice.client.common.controller.ReservationServiceController;
import com.fcscs.cservice.client.common.model.folio.FolioSearchItemsGWTEnum;
import com.fcscs.cservice.client.common.model.folio.FolioSearchItemsGWTEnumModelData;
import com.fcscs.cservice.client.common.model.folio.SearchItemsModel;
import com.fcscs.cservice.client.common.model.reservation.ReservationStatusModelData;
import com.fcscs.cservice.client.common.model.reservation.ReservationViewModelData;
import com.fcscs.cservice.domain.model.ReservationConfView;
import com.fcscs.cservice.domain.model.ReservationConfView.ReservationStatus;
import com.fcscs.cservice.domain.model.ReservationView;
import com.fcscs.share.gwt.common.ShareGWTConstants;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Widget;

public class FolioPanel extends FcsPanel {
	private VerticalPanel vp;
	private FolioGrid grid;
	private List<ReservationViewModelData> reservationViewModelList;
	private SearchItemsModel searchItemsModel;
	
	private ListStore<FolioSearchItemsGWTEnumModelData> searchItemListStore;
	private ListStore<FolioSearchItemsGWTEnumModelData> searchItemListStore1;
	private ListStore<FolioSearchItemsGWTEnumModelData> searchItemListStore2;
	private ListStore<FolioSearchItemsGWTEnumModelData> searchItemListStore3;
	private LayoutContainer lc1;
	private LayoutContainer lc2;
	private LayoutContainer lc3;
	private LayoutContainer lc4;
	
	private EasyClearCombobox<FolioSearchItemsGWTEnumModelData> searchItemCombo1;
	private EasyClearCombobox<FolioSearchItemsGWTEnumModelData> searchItemCombo2;
	private EasyClearCombobox<FolioSearchItemsGWTEnumModelData> searchItemCombo3;
	
	private TextField<String> firstName1;
	private TextField<String> firstName2;
	private TextField<String> firstName3;
	private TextField<String> lastName1;
	private TextField<String> lastName2;
	private TextField<String> lastName3;
	private EasyClearCombobox<ReservationStatusModelData> reservationStatusCombo1;
	private EasyClearCombobox<ReservationStatusModelData> reservationStatusCombo2;
	private EasyClearCombobox<ReservationStatusModelData> reservationStatusCombo3;
	protected ListStore<ReservationStatusModelData> reservationStatusListStore;
	private TextField<String> roomNo1;
	private TextField<String> roomNo2;
	private TextField<String> roomNo3;
	private IEFriendlyDateField dateField11;
	private IEFriendlyDateField dateField12;
	private IEFriendlyDateField dateField21;
	private IEFriendlyDateField dateField22;
	private IEFriendlyDateField dateField31;
	private IEFriendlyDateField dateField32;
	
	private FormButton searchButton;
	
	public FolioPanel(String id) {
		super(id);
	}

	@Override
	public void init() {
        if(null == vp){
        	vp = new VerticalPanel();
        	vp.setId("FolioPanel_vp");
			vp.setSpacing(10);
			
			FormPanel panel = new FormPanel();
			panel.setId("FolioPanel_panel");
			panel.setFrame(true);
			panel.setHeading(CServiceConstants.CSERVICE_CONSTANTS.searchCriteria());
			panel.setPadding(0);
			panel.setSize(950, 150);
			panel.setLabelAlign(LabelAlign.LEFT);
//			panel.setLayout(new ColumnLayout());
			
			searchItemListStore = new ListStore<FolioSearchItemsGWTEnumModelData>();
			for(FolioSearchItemsGWTEnum fsi : FolioSearchItemsGWTEnum.values()){
				searchItemListStore.add(new FolioSearchItemsGWTEnumModelData(fsi));
			}
			
			searchItemListStore1 = new ListStore<FolioSearchItemsGWTEnumModelData>();
			searchItemListStore2 = new ListStore<FolioSearchItemsGWTEnumModelData>();
			searchItemListStore3 = new ListStore<FolioSearchItemsGWTEnumModelData>();
			
			lc1 = new LayoutContainer();
			lc1.setId("FolioPanel_lc1");
			lc1.setLayout(new ColumnLayout());
			
			lc2 = new LayoutContainer();
			lc2.setId("FolioPanel_lc2");
			lc2.setLayout(new ColumnLayout());
			
			lc3 = new LayoutContainer();
			lc3.setId("FolioPanel_lc3");
			lc3.setLayout(new ColumnLayout());
			
			lc4 = new LayoutContainer();
			lc4.setId("FolioPanel_lc4");
			lc4.setLayout(new ColumnLayout());
			
			searchItemCombo1 = new EasyClearCombobox<FolioSearchItemsGWTEnumModelData>();
			searchItemCombo1.setId("FolioPanel_searchItemCombo1");
			searchItemCombo1.setStore(searchItemListStore1);
			searchItemCombo1.setValue(searchItemListStore.getAt(0));
			searchItemCombo1.setTriggerAction(TriggerAction.ALL);
			searchItemCombo1.setDisplayField(CoreConstants.FULLTNAME);
			searchItemCombo1.setForceSelection(true);
			searchItemCombo1.setAllowBlank(false);
			searchItemCombo1.addSelectionChangedListener(new SelectionChangedListener<FolioSearchItemsGWTEnumModelData>(){
				@Override
				public void selectionChanged(
						SelectionChangedEvent<FolioSearchItemsGWTEnumModelData> se) {
					searchItemView(searchItemCombo1.getValue().getEnum().getIndex(), 1);
					buildStore();
				}
			});
			
			searchItemCombo2 = new EasyClearCombobox<FolioSearchItemsGWTEnumModelData>();
			searchItemCombo2.setId("FolioPanel_searchItemCombo2");
			searchItemCombo2.setStore(searchItemListStore2);
			searchItemCombo2.setValue(searchItemListStore.getAt(1));
			searchItemCombo2.setTriggerAction(TriggerAction.ALL);
			searchItemCombo2.setDisplayField(CoreConstants.FULLTNAME);
			searchItemCombo2.setForceSelection(true);
			searchItemCombo2.setAllowBlank(false);
			searchItemCombo2.addSelectionChangedListener(new SelectionChangedListener<FolioSearchItemsGWTEnumModelData>(){
				@Override
				public void selectionChanged(
						SelectionChangedEvent<FolioSearchItemsGWTEnumModelData> se) {
					searchItemView(searchItemCombo2.getValue().getEnum().getIndex(), 2);
					buildStore();
				}
			});
			
			searchItemCombo3 = new EasyClearCombobox<FolioSearchItemsGWTEnumModelData>();
			searchItemCombo3.setId("FolioPanel_searchItemCombo3");
			searchItemCombo3.setStore(searchItemListStore3);
			searchItemCombo3.setValue(searchItemListStore.getAt(2));
			searchItemCombo3.setTriggerAction(TriggerAction.ALL);
			searchItemCombo3.setDisplayField(CoreConstants.FULLTNAME);
			searchItemCombo3.setForceSelection(true);
			searchItemCombo3.setAllowBlank(false);
			searchItemCombo3.addSelectionChangedListener(new SelectionChangedListener<FolioSearchItemsGWTEnumModelData>(){
				@Override
				public void selectionChanged(
						SelectionChangedEvent<FolioSearchItemsGWTEnumModelData> se) {
					searchItemView(searchItemCombo3.getValue().getEnum().getIndex(), 3);
					buildStore();
				}
			});
			
			lc1.add(searchItemCombo1, new ColumnData(150));
			lc1.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "60px");
			lc2.add(searchItemCombo2, new ColumnData(150));
			lc2.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "60px");
			lc2.setStyleAttribute(CServiceConstants.CSS_MARGINTOP, "20px");
			lc3.add(searchItemCombo3, new ColumnData(150));
			lc3.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "60px");
			lc3.setStyleAttribute(CServiceConstants.CSS_MARGINTOP, "20px");
			
			firstName1 = new TextField<String>();
			firstName1.setId("FolioPanel_firstName1");
			firstName1.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			firstName1.setEmptyText(CoreConstants.CORE_CONSTANTS.firstname());
			firstName1.setVisible(false);
			firstName1.setValidator(new Validator(){
				@Override
				public String validate(Field<?> field, String value) {
					if(!firstName1.isVisible()){
						return null;
					}else if(null != value && (!value.trim().isEmpty()) &&(value.trim().length() < 2 || value.trim().length() > 255)){
						return ShareGWTConstants.SHARE_GWT_MESSAGES.nameValid(CoreConstants.CORE_CONSTANTS.firstname(), 2, 255);
					}
					return null;
				}
			});
			
			firstName2 = new TextField<String>();
			firstName2.setId("FolioPanel_firstName2");
			firstName2.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			firstName2.setEmptyText(CoreConstants.CORE_CONSTANTS.firstname());
			firstName2.setVisible(false);
			firstName2.setValidator(new Validator(){
				@Override
				public String validate(Field<?> field, String value) {
					if(!firstName2.isVisible()){
						return null;
					}else if(null != value && (!value.trim().isEmpty()) &&(value.trim().length() < 2 || value.trim().length() > 255)){
						return ShareGWTConstants.SHARE_GWT_MESSAGES.nameValid(CoreConstants.CORE_CONSTANTS.firstname(), 2, 255);
					}
					return null;
				}
			});
			
			firstName3 = new TextField<String>();
			firstName3.setId("FolioPanel_firstName3");
			firstName3.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			firstName3.setEmptyText(CoreConstants.CORE_CONSTANTS.firstname());
			firstName3.setVisible(false);
			firstName3.setValidator(new Validator(){
				@Override
				public String validate(Field<?> field, String value) {
					if(!firstName3.isVisible()){
						return null;
					}else if(null != value && (!value.trim().isEmpty()) &&(value.trim().length() < 2 || value.trim().length() > 255)){
						return ShareGWTConstants.SHARE_GWT_MESSAGES.nameValid(CoreConstants.CORE_CONSTANTS.firstname(), 2, 255);
					}
					return null;
				}
			});
			
			lc1.add(firstName1, new ColumnData(150));
			lc2.add(firstName2, new ColumnData(150));
			lc3.add(firstName3, new ColumnData(150));
			
			lastName1 = new TextField<String>();
			lastName1.setId("FolioPanel_lastName1");
			lastName1.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			lastName1.setEmptyText(CoreConstants.CORE_CONSTANTS.lastname());
			lastName1.setVisible(false);
			lastName1.setValidator(new Validator(){
				@Override
				public String validate(Field<?> field, String value) {
					if(!lastName1.isVisible()){
						return null;
					}else if(null != value && (!value.trim().isEmpty()) &&(value.trim().length() < 2 || value.trim().length() > 255)){
						return ShareGWTConstants.SHARE_GWT_MESSAGES.nameValid(CoreConstants.CORE_CONSTANTS.lastname(), 2, 255);
					}
					return null;
				}
			});
			
			lastName2 = new TextField<String>();
			lastName2.setId("FolioPanel_lastName2");
			lastName2.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			lastName2.setEmptyText(CoreConstants.CORE_CONSTANTS.lastname());
			lastName2.setVisible(false);
			lastName2.setValidator(new Validator(){
				@Override
				public String validate(Field<?> field, String value) {
					if(!lastName2.isVisible()){
						return null;
					}else if(null != value && (!value.trim().isEmpty()) &&(value.trim().length() < 2 || value.trim().length() > 255)){
						return ShareGWTConstants.SHARE_GWT_MESSAGES.nameValid(CoreConstants.CORE_CONSTANTS.lastname(), 2, 255);
					}
					return null;
				}
			});
			
			lastName3 = new TextField<String>();
			lastName3.setId("FolioPanel_lastName3");
			lastName3.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			lastName3.setEmptyText(CoreConstants.CORE_CONSTANTS.lastname());
			lastName3.setVisible(false);
			lastName3.setValidator(new Validator(){
				@Override
				public String validate(Field<?> field, String value) {
					if(!lastName3.isVisible()){
						return null;
					}else if(null != value && (!value.trim().isEmpty()) &&(value.trim().length() < 2 || value.trim().length() > 255)){
						return ShareGWTConstants.SHARE_GWT_MESSAGES.nameValid(CoreConstants.CORE_CONSTANTS.lastname(), 2, 255);
					}
					return null;
				}
			});
			
			lc1.add(lastName1, new ColumnData(150));
			lc2.add(lastName2, new ColumnData(150));
			lc3.add(lastName3, new ColumnData(150));
			
			reservationStatusListStore = new ListStore<ReservationStatusModelData>();
			reservationStatusCombo1 = new EasyClearCombobox<ReservationStatusModelData>();
			reservationStatusCombo1.setId("FolioPanel_reservationStatusCombobox1");
			reservationStatusCombo1.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			reservationStatusCombo1.setEmptyText(CServiceConstants.CSERVICE_CONSTANTS.reservationStatus());
			reservationStatusCombo1.setDisplayField(CServiceConstants.STATUS);
			reservationStatusCombo1.setTriggerAction(TriggerAction.ALL);
			reservationStatusCombo1.setStore(reservationStatusListStore);
			reservationStatusCombo1.setForceSelection(true);
			reservationStatusCombo1.setEditable(false);
			reservationStatusCombo1.setVisible(false);
			
			reservationStatusCombo2 = new EasyClearCombobox<ReservationStatusModelData>();
			reservationStatusCombo2.setId("FolioPanel_reservationStatusCombo2");
			reservationStatusCombo2.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			reservationStatusCombo2.setEmptyText(CServiceConstants.CSERVICE_CONSTANTS.reservationStatus());
			reservationStatusCombo2.setDisplayField(CServiceConstants.STATUS);
			reservationStatusCombo2.setTriggerAction(TriggerAction.ALL);
			reservationStatusCombo2.setStore(reservationStatusListStore);
			reservationStatusCombo2.setForceSelection(true);
			reservationStatusCombo2.setEditable(false);
			reservationStatusCombo2.setVisible(false);
			
			reservationStatusCombo3 = new EasyClearCombobox<ReservationStatusModelData>();
			reservationStatusCombo3.setId("FolioPanel_reservationStatusCombo3");
			reservationStatusCombo3.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			reservationStatusCombo3.setEmptyText(CServiceConstants.CSERVICE_CONSTANTS.reservationStatus());
			reservationStatusCombo3.setDisplayField(CServiceConstants.STATUS);
			reservationStatusCombo3.setTriggerAction(TriggerAction.ALL);
			reservationStatusCombo3.setStore(reservationStatusListStore);
			reservationStatusCombo3.setForceSelection(true);
			reservationStatusCombo3.setEditable(false);
			reservationStatusCombo3.setVisible(false);
			
			lc1.add(reservationStatusCombo1, new ColumnData(150));
			lc2.add(reservationStatusCombo2, new ColumnData(150));
			lc3.add(reservationStatusCombo3, new ColumnData(150));
			
			roomNo1 = new TextField<String>();
			roomNo1.setId("FolioPanel_roomNo1");
			roomNo1.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			roomNo1.setVisible(false);
			
			roomNo2 = new TextField<String>();
			roomNo2.setId("FolioPanel_roomNo2");
			roomNo2.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			roomNo2.setVisible(false);
			
			roomNo3 = new TextField<String>();
			roomNo3.setId("FolioPanel_roomNo3");
			roomNo3.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			roomNo3.setVisible(false);
			
			lc1.add(roomNo1, new ColumnData(150));
			lc2.add(roomNo2, new ColumnData(150));
			lc3.add(roomNo3, new ColumnData(150));
			
			dateField11 = new IEFriendlyDateField();
			dateField11.setId("FolioPanel_dateField11");
			dateField11.setEmptyText(CoreConstants.CORE_CONSTANTS.form() + " " + CoreConstants.CORE_CONSTANTS.date());
			dateField11.setMessageTarget("tooltip");
			dateField11.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			dateField11.setEditable(false);
			dateField11.setVisible(false);
			
			dateField12 = new IEFriendlyDateField();
			dateField12.setId("FolioPanel_dateField12");
			dateField12.setEmptyText(CoreConstants.CORE_CONSTANTS.to() + " " + CoreConstants.CORE_CONSTANTS.date());
			dateField12.setMessageTarget("tooltip");
			dateField12.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			dateField12.setEditable(false);
			dateField12.setVisible(false);
			
			dateField21 = new IEFriendlyDateField();
			dateField21.setId("FolioPanel_dateField21");
			dateField21.setEmptyText(CoreConstants.CORE_CONSTANTS.form() + " " + CoreConstants.CORE_CONSTANTS.date());
			dateField21.setMessageTarget("tooltip");
			dateField21.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			dateField21.setEditable(false);
			dateField21.setVisible(false);
			
			dateField22 = new IEFriendlyDateField();
			dateField22.setId("FolioPanel_dateField22");
			dateField22.setEmptyText(CoreConstants.CORE_CONSTANTS.to() + " " + CoreConstants.CORE_CONSTANTS.date());
			dateField22.setMessageTarget("tooltip");
			dateField22.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			dateField22.setEditable(false);
			dateField22.setVisible(false);
			
			dateField31 = new IEFriendlyDateField();
			dateField31.setId("FolioPanel_dateField31");
			dateField31.setEmptyText(CoreConstants.CORE_CONSTANTS.form() + " " + CoreConstants.CORE_CONSTANTS.date());
			dateField31.setMessageTarget("tooltip");
			dateField31.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			dateField31.setEditable(false);
			dateField31.setVisible(false);
			
			dateField32 = new IEFriendlyDateField();
			dateField32.setId("FolioPanel_dateField32");
			dateField32.setEmptyText(CoreConstants.CORE_CONSTANTS.to() + " " + CoreConstants.CORE_CONSTANTS.date());
			dateField32.setMessageTarget("tooltip");
			dateField32.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			dateField32.setEditable(false);
			dateField32.setVisible(false);
			
			lc1.add(dateField11, new ColumnData(150));
			lc1.add(dateField12, new ColumnData(150));
			lc2.add(dateField21, new ColumnData(150));
			lc2.add(dateField22, new ColumnData(150));
			lc3.add(dateField31, new ColumnData(150));
			lc3.add(dateField32, new ColumnData(150));
			
			searchButton=new FormButton(CoreConstants.CORE_CONSTANTS.search());
			searchButton.setId("FolioPanel_searchButton");
			searchButton.addSelectionListener(new SelectionListener<ButtonEvent>() {

				@Override
				public void componentSelected(ButtonEvent ce) {
					grid.clear();
					boolean isValid;
					if((firstName1.isVisible() && !firstName1.isValid()) 
						|| (firstName2.isVisible() && !firstName2.isValid())
						|| (firstName3.isVisible() && !firstName3.isValid())
						|| (lastName1.isVisible() && !lastName1.isValid())
						|| (lastName2.isVisible() && !lastName2.isValid())
						|| (lastName3.isVisible() && !lastName3.isValid())){
						isValid = false;
					}else{
						isValid = true;
					}
					
					if(isValid){
						newSearch(true);
					}
				}
			});
			lc4.add(searchButton);
			lc4.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "600px");
			
			panel.add(lc1);
			panel.add(lc2);
			panel.add(lc3);
			panel.add(lc4);
			
			
			vp.add(panel);
			
			grid = FolioGrid.getINSTANCE(
				CServiceConstants.CSERVICE_CONSTANTS.reservationView(), 
				new SearchExecutor<ReservationViewModelData, MainPaginationSearchModel>(){
					@Override
					public void doSearch(
							final MainPaginationSearchModel searchModel,
							final AsyncCallback<BasePagingLoadResult<ReservationViewModelData>> callback) {
						if(null == searchModel){
							callback.onSuccess(createEmptyResult(new ArrayList<ReservationViewModelData>()));
							return;
						}
						
						searchItemsModel = new SearchItemsModel();
						
						createSearchTerm(searchItemCombo1.getValue().getEnum().getIndex(), 1);
						createSearchTerm(searchItemCombo2.getValue().getEnum().getIndex(), 2);
						createSearchTerm(searchItemCombo3.getValue().getEnum().getIndex(), 3);
						
						ReservationServiceController.getINSTANCE().findRsvForFolio(
							searchItemsModel.getFirstName(), 
							searchItemsModel.getLastName(), 
							searchItemsModel.getRsvStatusId(), 
							searchItemsModel.getRoomNo(), 
							searchItemsModel.getArrivStart(), 
							searchItemsModel.getArrivEnd(), 
							searchItemsModel.getDepartStart(), 
							searchItemsModel.getDepartEnd(), 
							new Date(), searchModel, 
							new AsyncCallbackListener<SearchResultsContainer<ReservationView>>(){
								@Override
								public void onLogin(String username) {
									ReservationServiceController.getINSTANCE().findRsvForFolio(
											searchItemsModel.getFirstName(), 
											searchItemsModel.getLastName(), 
											searchItemsModel.getRsvStatusId(), 
											searchItemsModel.getRoomNo(), 
											searchItemsModel.getArrivStart(), 
											searchItemsModel.getArrivEnd(), 
											searchItemsModel.getDepartStart(), 
											searchItemsModel.getDepartEnd(), 
											new Date(), searchModel,this);
								}

								@Override
								public void onFailure(Throwable caught) {
									MessageBox.alert(CoreConstants.CORE_MESSAGES.operation(CoreConstants.CORE_CONSTANTS.system(), ShareGWTConstants.SHARE_GWT_CONSTANTS.error()),
											CoreConstants.CORE_CONSTANTS.systemError(), null);
								}

								@Override
								public void onSuccess(
										SearchResultsContainer<ReservationView> result) {
									if (null != result) {
										reservationViewModelList = new ArrayList<ReservationViewModelData>();
										for (ReservationView rv : result.getResults()) {
											reservationViewModelList.add(new ReservationViewModelData(rv));
										}
										callback.onSuccess(FcsPanel.createPagingLoadResult(reservationViewModelList,
											searchModel.getCurrentPage(),searchModel.getResultsPerPage(),
										    result.getTotalCnt()));
									}
								}
								
							});
					}
				});
			grid.setId("FolioPanel_grid");
			vp.add(grid);
			
			add(vp);
        }
        searchItemView(0, 1);
		searchItemView(1, 2);
		searchItemView(2, 3);
		buildStore();
		onShow();
	}

	@Override
	public UserAuthority getRequiredAuthority() {
		return CServiceAuthority.cSERVICE_RESV;
	}

	@Override
	protected Widget getViewPanel() {
		return vp;
	}

	@Override
	public void onShow() {
		newSearch(true);
		loadReservConf();
	}

	@Override
	public boolean onHide() {
		if(firstName1.isVisible() && firstName1.getValue() != null && !firstName1.getValue().trim().isEmpty()){
			firstName1.clear();
		}else if(firstName2.isVisible() && firstName2.getValue() != null && !firstName2.getValue().trim().isEmpty()){
			firstName2.clear();
		}else if(firstName3.isVisible() && firstName3.getValue() != null && !firstName3.getValue().trim().isEmpty()){
			firstName3.clear();
		}
		
		if(lastName1.isVisible() && lastName1.getValue() != null && !lastName1.getValue().trim().isEmpty()){
			lastName1.clear();
		}else if(lastName2.isVisible() && lastName2.getValue() != null && !lastName2.getValue().trim().isEmpty()){
			lastName2.clear();
		}else if(lastName3.isVisible() && lastName3.getValue() != null && !lastName3.getValue().trim().isEmpty()){
			lastName3.clear();
		}
		
		if(reservationStatusCombo1.isVisible() && reservationStatusCombo1.getValue() != null){
			reservationStatusCombo1.clear();
		}else if(reservationStatusCombo2.isVisible() && reservationStatusCombo2.getValue() != null){
			reservationStatusCombo2.clear();
		}else if(reservationStatusCombo3.isVisible() && reservationStatusCombo3.getValue() != null){
			reservationStatusCombo3.clear();
		}
		
		if(roomNo1.isVisible() && roomNo1.getValue() != null && !roomNo1.getValue().trim().isEmpty()){
			roomNo1.clear();
		}else if(roomNo2.isVisible() && roomNo2.getValue() != null && !roomNo2.getValue().trim().isEmpty()){
			roomNo2.clear();
		}else if(roomNo3.isVisible() && roomNo3.getValue() != null && !roomNo3.getValue().trim().isEmpty()){
			roomNo3.clear();
		}
		
		if(dateField11.isVisible() && dateField11.getValue() != null){
			dateField11.clear();
		}
		if(dateField21.isVisible() && dateField21.getValue() != null){
			dateField21.clear();
		}
		if(dateField31.isVisible() && dateField31.getValue() != null){
			dateField31.clear();
		}
		if(dateField12.isVisible() && dateField12.getValue() != null){
			dateField12.clear();
		}
		if(dateField22.isVisible() && dateField22.getValue() != null){
			dateField22.clear();
		}
		if(dateField32.isVisible() && dateField32.getValue() != null){
			dateField32.clear();
		}
		
		return true;
	}
	
	private void buildStore() {
		searchItemListStore1.removeAll();
		for(int i = 0; i < searchItemListStore.getCount() ; i++){
			if((searchItemCombo2.getValue().getEnum().getIndex() 
				!= searchItemListStore.getAt(i).getEnum().getIndex())
				&&
				(searchItemCombo3.getValue().getEnum().getIndex() 
						!= searchItemListStore.getAt(i).getEnum().getIndex())){
				searchItemListStore1.add(searchItemListStore.getAt(i));
			}
		}
		
		searchItemListStore3.removeAll();
		for(int i = 0; i < searchItemListStore.getCount() ; i++){
			if((searchItemCombo1.getValue().getEnum().getIndex() 
					!= searchItemListStore.getAt(i).getEnum().getIndex())
					&&
					(searchItemCombo2.getValue().getEnum().getIndex() 
							!= searchItemListStore.getAt(i).getEnum().getIndex())){
				searchItemListStore3.add(searchItemListStore.getAt(i));
			}
		}
		
		searchItemListStore2.removeAll();
		for(int i = 0; i < searchItemListStore.getCount() ; i++){
			if((searchItemCombo1.getValue().getEnum().getIndex() 
					!= searchItemListStore.getAt(i).getEnum().getIndex())
					&&
					(searchItemCombo3.getValue().getEnum().getIndex() 
							!= searchItemListStore.getAt(i).getEnum().getIndex())){
				searchItemListStore2.add(searchItemListStore.getAt(i));
			}
		}
	}

	private void searchItemView(int code,int row){
		switch(code){
		    case 0:
		    	switch(row){
		    	    case 1:
		    	    	firstName1.clear();
		    	    	lastName1.clear();
		    	    	firstName1.setVisible(true);
		    	    	lastName1.setVisible(true);
		    	    	reservationStatusCombo1.setVisible(false);
		    	    	roomNo1.setVisible(false);
		    	    	dateField11.setVisible(false);
		    	    	dateField12.setVisible(false);
		    	    	break;
		    	    case 2:
		    	    	firstName2.clear();
		    	    	lastName2.clear();
		    	    	firstName2.setVisible(true);
		    	    	lastName2.setVisible(true);
		    	    	reservationStatusCombo2.setVisible(false);
		    	    	roomNo2.setVisible(false);
		    	    	dateField21.setVisible(false);
		    	    	dateField22.setVisible(false);
		    	    	break;
		    	    case 3:
		    	    	firstName3.clear();
		    	    	lastName3.clear();
		    	    	firstName3.setVisible(true);
		    	    	lastName3.setVisible(true);
		    	    	reservationStatusCombo3.setVisible(false);
		    	    	roomNo3.setVisible(false);
		    	    	dateField31.setVisible(false);
		    	    	dateField32.setVisible(false);
		    	    	break;
		    	}
		    	break;
		    case 1:
		    	switch(row){
	    	    case 1:
	    	    	reservationStatusCombo1.clear();
	    	    	firstName1.setVisible(false);
	    	    	lastName1.setVisible(false);
	    	    	reservationStatusCombo1.setVisible(true);
	    	    	roomNo1.setVisible(false);
	    	    	dateField11.setVisible(false);
	    	    	dateField12.setVisible(false);
	    	    	break;
	    	    case 2:
	    	    	reservationStatusCombo2.clear();
	    	    	firstName2.setVisible(false);
	    	    	lastName2.setVisible(false);
	    	    	reservationStatusCombo2.setVisible(true);
	    	    	roomNo2.setVisible(false);
	    	    	dateField21.setVisible(false);
	    	    	dateField22.setVisible(false);
	    	    	break;
	    	    case 3:
	    	    	reservationStatusCombo3.clear();
	    	    	firstName3.setVisible(false);
	    	    	lastName3.setVisible(false);
	    	    	reservationStatusCombo3.setVisible(true);
	    	    	roomNo3.setVisible(false);
	    	    	dateField31.setVisible(false);
	    	    	dateField32.setVisible(false);
	    	    	break;
	    	    }
	    	    break;
		    case 2:
		    	switch(row){
	    	    case 1:
	    	    	roomNo1.clear();
	    	    	firstName1.setVisible(false);
	    	    	lastName1.setVisible(false);
	    	    	reservationStatusCombo1.setVisible(false);
	    	    	roomNo1.setVisible(true);
	    	    	dateField11.setVisible(false);
	    	    	dateField12.setVisible(false);
	    	    	break;
	    	    case 2:
	    	    	roomNo2.clear();
	    	    	firstName2.setVisible(false);
	    	    	lastName2.setVisible(false);
	    	    	reservationStatusCombo2.setVisible(false);
	    	    	roomNo2.setVisible(true);
	    	    	dateField21.setVisible(false);
	    	    	dateField22.setVisible(false);
	    	    	break;
	    	    case 3:
	    	    	roomNo3.clear();
	    	    	firstName3.setVisible(false);
	    	    	lastName3.setVisible(false);
	    	    	reservationStatusCombo3.setVisible(false);
	    	    	roomNo3.setVisible(true);
	    	    	dateField31.setVisible(false);
	    	    	dateField32.setVisible(false);
	    	    	break;
	    	    }
	    	    break;
		    case 3:
		    case 4:
		    	switch(row){
	    	    case 1:
	    	    	dateField11.clear();
	    	    	dateField12.clear();
	    	    	firstName1.setVisible(false);
	    	    	lastName1.setVisible(false);
	    	    	reservationStatusCombo1.setVisible(false);
	    	    	roomNo1.setVisible(false);
	    	    	dateField11.setVisible(true);
	    	    	dateField12.setVisible(true);
	    	    	break;
	    	    case 2:
	    	    	dateField21.clear();
	    	    	dateField22.clear();
	    	    	firstName2.setVisible(false);
	    	    	lastName2.setVisible(false);
	    	    	reservationStatusCombo2.setVisible(false);
	    	    	roomNo2.setVisible(false);
	    	    	dateField21.setVisible(true);
	    	    	dateField22.setVisible(true);
	    	    	break;
	    	    case 3:
	    	    	dateField31.clear();
	    	    	dateField32.clear();
	    	    	firstName3.setVisible(false);
	    	    	lastName3.setVisible(false);
	    	    	reservationStatusCombo3.setVisible(false);
	    	    	roomNo3.setVisible(false);
	    	    	dateField31.setVisible(true);
	    	    	dateField32.setVisible(true);
	    	    	break;
	    	    }
	    	    break;
		}
	}
	
	private void loadReservConf(){
		reservationStatusListStore.removeAll();
		CServiceCommonServiceController.getINSTANCE().getRsvConfView(
				SessionContext.getInstance().getPropertyId(), 
				new AsyncCallbackListener<ReservationConfView>(){
					@Override
					public void onLogin(String username) {
						CServiceCommonServiceController.getINSTANCE().getRsvConfView(
							SessionContext.getInstance().getPropertyId(),this);
					}
					@Override
					public void onFailure(Throwable caught) {
						MessageBox.alert(CoreConstants.CORE_MESSAGES.operation(CoreConstants.CORE_CONSTANTS.system(), ShareGWTConstants.SHARE_GWT_CONSTANTS.error()),
							CoreConstants.CORE_CONSTANTS.systemError(), null);
					}
					@Override
					public void onSuccess(ReservationConfView result) {
						if(null != result){
							grid.setReservConf(result);
							for(ReservationStatus rs : result.getReservationStatusList()){
								reservationStatusListStore.add(new ReservationStatusModelData(rs));
							}
						}
					}
				});
	}
	
	protected void newSearch(boolean all) {
		String term = null;
		MainPaginationSearchModel sm = new MainPaginationSearchModel();
		sm.setSearchTerm(term);
		grid.newSearch(sm);
	}
	
	private void createSearchTerm(int code,int row){
		switch(code){
		    case 0:
		    	switch(row){
		    	    case 1:
					    if (firstName1.getValue() != null) {
						    searchItemsModel.setFirstName(firstName1.getValue().trim());
					    }else{
						    searchItemsModel.setFirstName(null);
					    }
					    if (lastName1.getValue() != null) {
						    searchItemsModel.setLastName(lastName1.getValue().trim());
					    }else{
						    searchItemsModel.setLastName(null);
					    }
					    break;
		    	    case 2:
		    	    	if (firstName2.getValue() != null) {
						    searchItemsModel.setFirstName(firstName2.getValue().trim());
					    }else{
						    searchItemsModel.setFirstName(null);
					    }
					    if (lastName2.getValue() != null) {
						    searchItemsModel.setLastName(lastName2.getValue().trim());
					    }else{
						    searchItemsModel.setLastName(null);
					    }
		    	    	break;
		    	    case 3:
		    	    	if (firstName3.getValue() != null) {
						    searchItemsModel.setFirstName(firstName3.getValue().trim());
					    }else{
						    searchItemsModel.setFirstName(null);
					    }
					    if (lastName3.getValue() != null) {
						    searchItemsModel.setLastName(lastName3.getValue().trim());
					    }else{
						    searchItemsModel.setLastName(null);
					    }
		    	    	break;
		    	}
		    	break;
		    case 1:
		    	switch(row){
	    	    case 1:
	    	    	if (reservationStatusCombo1.getValue() != null) {
					    searchItemsModel.setRsvStatusId(reservationStatusCombo1.getValue().getEntity().getReservStatusId());
				    }else{
					    searchItemsModel.setRsvStatusId(null);
				    }
	    	    	break;
	    	    case 2:
	    	    	if (reservationStatusCombo2.getValue() != null) {
					    searchItemsModel.setRsvStatusId(reservationStatusCombo2.getValue().getEntity().getReservStatusId());
				    }else{
					    searchItemsModel.setRsvStatusId(null);
				    }
	    	    	break;
	    	    case 3:
	    	    	if (reservationStatusCombo3.getValue() != null) {
					    searchItemsModel.setRsvStatusId(reservationStatusCombo3.getValue().getEntity().getReservStatusId());
				    }else{
					    searchItemsModel.setRsvStatusId(null);
				    }
	    	    	break;
	    	    }
	    	    break;
		    case 2:
		    	switch(row){
	    	    case 1:
	    	    	if (roomNo1.getValue() != null) {
					    searchItemsModel.setRoomNo(roomNo1.getValue().trim());
				    }else{
					    searchItemsModel.setRoomNo(null);
				    }
	    	    	break;
	    	    case 2:
	    	    	if (roomNo2.getValue() != null) {
					    searchItemsModel.setRoomNo(roomNo2.getValue().trim());
				    }else{
					    searchItemsModel.setRoomNo(null);
				    }
	    	    	break;
	    	    case 3:
	    	    	if (roomNo3.getValue() != null) {
					    searchItemsModel.setRoomNo(roomNo3.getValue().trim());
				    }else{
					    searchItemsModel.setRoomNo(null);
				    }
	    	    	break;
	    	    }
	    	    break;
		    case 3:
		    	switch(row){
	    	    case 1:
					searchItemsModel.setArrivStart(dateField11.getValue());
					searchItemsModel.setArrivEnd(dateField12.getValue());
	    	    	break;
	    	    case 2:
	    	    	searchItemsModel.setArrivStart(dateField21.getValue());
					searchItemsModel.setArrivEnd(dateField22.getValue());
	    	    	break;
	    	    case 3:
	    	    	searchItemsModel.setArrivStart(dateField31.getValue());
					searchItemsModel.setArrivEnd(dateField32.getValue());
	    	    	break;
	    	    }
	    	    break;
		    case 4:
		    	switch(row){
	    	    case 1:
	    	    	searchItemsModel.setDepartStart(dateField11.getValue());
	    	    	searchItemsModel.setDepartEnd(dateField12.getValue());
	    	    	break;
	    	    case 2:
	    	    	searchItemsModel.setDepartStart(dateField21.getValue());
	    	    	searchItemsModel.setDepartEnd(dateField22.getValue());
	    	    	break;
	    	    case 3:
	    	    	searchItemsModel.setDepartStart(dateField31.getValue());
	    	    	searchItemsModel.setDepartEnd(dateField32.getValue());
	    	    	break;
	    	    }
	    	    break;
		}
	}
}
